<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>保险数据分析</title>
  <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">
  
  <!-- 引入 ECharts 文件 -->
  <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script>
  
  <style>
      body {
          font-family: Arial, sans-serif;
          margin: 0;
          padding: 20px;
      }
      .chart-container {
          width: 100%;
          max-width: 800px;
          height: 400px; /* 设置固定高度 */
          margin: 0 auto 40px;
      }
  </style>
</head>
<body>
  <div class="container mt-5">
    <h1 class="text-center mb-4">保险报价数据分析</h1>
    <nav class="text-center mb-4" style="margin-bottom: 20px;">
        <button id="QuotePage" class="btn btn-secondary">险种报价</button>
        <button id="Dashboard" class="btn btn-primary">报表页面</button>
    </nav>
      <!-- 图表容器 -->
    <div class="chart-container" id="chart1"></div>
    <div class="chart-container" id="chart2"></div>
    <div class="chart-container" id="chart3"></div>

  </div>
  <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
  
  <script type="text/javascript">
      
      $(document).ready(function() {
          // 点击险种报价
          $("#QuotePage").on("click", function() {
              window.location.href = "http://localhost:5000/";
          });

          // 点击报表
          $("#Dashboard").on("click", function() {
              window.location.href = "http://localhost:5000/dashboard";
          });
      });

      
      $(document).ready(function() {
        // chat1 chat2
        fetch("{{ url_for('dashboard.chart1') }}")
        .then(response => response.json())
        .then(data => {
            var counts = data.map(item => item.counts);
            var types = data.map(item => item.types);

            // 初始化图表
            var chart1 = echarts.init(document.getElementById('chart1'));
            var option1 = {
              title: {
                  text: '按类型划分的购买保单数量'
              },
              xAxis: {
                  type: 'category',
                  data: types
              },
              yAxis: {
                  type: 'value'
              },
              series: [{
                  data: counts,
                  type: 'bar'
              }]
            };

            chart1.setOption(option1);

            // 配置饼图
            var chart2 = echarts.init(document.getElementById('chart2'));
            const option2 = {
                title: {
                    text: '按类型划分的保单购买比例'
                },
                tooltip: {
                    trigger: 'item'
                },
                legend: {
                    top: '5%'
                },
                series: [
                    {
                        name: '保险类型',
                        type: 'pie',
                        radius: '50%',
                        data: data.map(item => ({
                            value: item.counts,
                            name: item.types
                        })),
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            chart2.setOption(option2);
        }).catch(error => console.error('Error fetching data:', error));


        fetch("{{ url_for('dashboard.chart3') }}")
        .then(response => response.json())
        .then(data => {
            // 初始化图表
            var chart3 = echarts.init(document.getElementById('chart3'));
            // 配置折线图
            var option3 = {
                title: {
                    text: '按保险类型划分的总保险金额'
                },
                xAxis: {
                    type: 'category',
                    data: data.map(item => item.types)
                },
                yAxis: {
                    type: 'value'
                },
                series: [{
                    data: data.map(item => item.total),
                    type: 'line',
                    smooth: true
                }]
            };

            chart3.setOption(option3);

        }).catch(error => console.error('Error fetching data:', error));

      })
  </script>


</body>
</html>